package com.example.demo;

import lombok.Data;

/**
 * 返回统一码
 *
 * @author 王明
 */
@Data
public class Result<T> {

    private String code;
    /**
     * 返回统一值
     */
    private boolean success = true;

    /**
     * 返回信息
     */
    private String msg = "success";

    /**
     * 返回数据
     */
    private T data;

    /**
     * 分页总数
     */
    private int total = 0;


    public void resultFail(String msg) {
        this.success = false;
        if (msg.contains("NullPointerException")){
            msg="空指针异常，请联系管理员";
        }
        if (msg.contains("TooManyResultsException")){
            msg="查询到多条数据，请联系管理员";
        }
        this.msg = msg;
    }

    public void resultSuccess(T obj) {
        this.success = true;
        this.data = obj;
    }

    /**
     * 自定义flag返回方法
     */
    public void resultFlag(Result<Integer> result, Integer flag, String trueDesc, String falseDesc) {
        if (flag > 0) {
            result.setSuccess(true);
            result.setMsg(trueDesc);
            result.setTotal(flag);
        } else {
            result.setSuccess(false);
            result.setMsg(falseDesc);
            result.setTotal(flag);
        }
    }

    public void resultFlagString(Result<String> result, Integer flag, String trueDesc, String falseDesc) {
        if (flag > 0) {
            result.setMsg(trueDesc);
            result.setTotal(flag);
        } else {
            result.setSuccess(false);
            result.setMsg(falseDesc);
            result.setTotal(flag);
        }
    }

}
